package com.cryann.mqttserver;

import com.cryann.mqttserver.modle.MqttMessageStore;
import com.cryann.mqttserver.mqttProcess.mqttMsgHandler.MqttMsgCheckHandler;
import com.cryann.mqttserver.repository.MessageRepository;
import com.cryann.mqttserver.repository.TopicRepository;
import com.cryann.mqttserver.service.MqttAccessor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cRyann
 * @date 2018/12/7 0007 10:06
 * @description:
 */
@RestController
@RequestMapping("/test")
@Api("localTest")
@Slf4j
public class TestSwagger {

    @Autowired
    MessageRepository messageRepository;
    @Autowired
    TopicRepository topicRepository;
    @Autowired
    MqttAccessor mqttAccessor;

    @Autowired
    MqttMsgCheckHandler mqttMsgCheckHandler;


    @ApiOperation(value = "install")
    @PostMapping(value = "install")
    public Object install(@RequestParam String msg, @RequestParam String topic) throws Exception {
        System.out.println();
        return mqttAccessor.getTopicNames(topic);
//        return "";
    }

    @Autowired
    MqttMessageStore mqttMessageStore;

    @ApiOperation(value = "getMsgList")
    @PostMapping(value = "getMsgList")
    public Object getMsgList() throws Exception {
        return mqttMessageStore.getMap();
//        return "";
    }

//    public List<Accessor> getTopicNames(String topicFromMsg) throws Exception {
//            List<Accessor> result = new ArrayList<>();
//            if (StringUtils.containsAny(topicFromMsg, "+", "#")) {
//                throw new Exception("无效的主题名：" + topicFromMsg + ",因为它包含了通配符");
//            }
//            List<Topic> topicList = topicRepository.findAll();
//            topicList.stream().forEach(t -> {
//                String regex = StringUtils.replace(t.getTopicName(), "+", ".");
//                regex = StringUtils.replace(regex, "#", ".*");
//                boolean isMatch = Pattern.matches(regex, topicFromMsg);
//                System.out.println("字符串中 " + regex + "<><><>" + isMatch);
//                if (isMatch) {
//                    result.addAll(t.getAccessors());
//                }
//            });
////        for (String ss : topic) {
////            String ssss = s + "/+";
////            String regex = "^" + s + "/" + ss + "|" + s + "/\\+";
////            s = s + "/" + ss;
////            String finalS = s;
////
////            System.err.println(regex);
////            List<Topic> topicList = topicRepository.findTopicByRegex(regex);
////
////            System.out.println(finalS);
////            topicList.stream().forEach(t -> {
////                String topicName = t.getTopicName();
////                String s1 = StringUtils.remove(topicName, finalS);
////                String s2 = StringUtils.remove(topicName, ssss);
////                System.err.println(topicName + "<><>" + s1 + "-----" + s2);
////                if (s1.equals("/#") || StringUtils.isBlank(s1) || StringUtils.isBlank(s2)) {
////                    result.addAll(t.getAccessors());
////                    System.out.println(result.size());
////                }
////            });
////        }
//
//        return result;
//    }
}
